package servlet.PropertyManagementModule.CostManagement;

import bean.dto.CostPage;
import bean.pojo.User;
import bean.vo.CostOption;
import bean.vo.UserVo;
import com.alibaba.fastjson.JSON;
import commons.r.R;
import service.CostManagementService;
import service.PeopleCostMangerService;
import service.UsersService;
import service.impl.CostManagementServiceImpl;
import service.impl.PeopleCostMangerServiceImpl;
import service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * servlet.PropertyManagementModule.CostManagement
 * User: xlh
 * Date: 2024/12/18 19:15
 * motto:推到高塔
 * Description:
 * Version: V1.0
 */
@WebServlet(name="CostTokenServlet",value="/peopleCostManage/findByPage")
public class PeopleCostMangerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String str_page = request.getParameter("page");
        String str_limit = request.getParameter("limit");
        String token = request.getParameter("token");
        String type_name = request.getParameter("typeId");
        String type_time = request.getParameter("selectMonth");
        System.out.println("token:"+token);
        User user =(User) getServletContext().getAttribute(token);
        //通过用户信息查询用户角色
        UsersService usersService = new UsersServiceImpl();
        UserVo vo=usersService.infoRole(user.getUserId());
        String tokenId= vo.getUserId().toString();
        //创建查询实体
        CostPage costPage= new CostPage();
        //判断是否有参数
        if(str_page.trim().length()>0){
            try {
                costPage.setPage(Integer.parseInt(str_page));
            }catch (Exception e){
                costPage.setPage(1);
            }
        }
        //设置每页行数
        if(str_limit.trim().length()>0){
            try {
                costPage.setLimit(Integer.parseInt(str_limit));
            }catch (Exception e){
                costPage.setLimit(10);
            }
        }
        //设置查询条件
        costPage.setTypeTime(type_time);
        costPage.setToken(tokenId);
        costPage.setTypeName(type_name);
        PeopleCostMangerService service=new PeopleCostMangerServiceImpl();
        //满足条件的总条数
        Long  total=service.findByTotal(costPage);
        //当前页码的数据
        List<CostOption> list=null;
        if(total>0) {
            //当前页码的数据
            list = service.findByPage(costPage);
        }
        //Data?     Long       addData把data HashMap
        R r= R.ok().addData("total",total).addData("list",list);
        //data={total:,list:} addData
        //响应对象
        PrintWriter writer = response.getWriter();
        String result = JSON.toJSONString(r);
        System.out.println("chazhao"+result);
        writer.write(result);
        writer.flush();
        writer.close();
    }
}


